package com.bdqn.servlet;

import com.bdqn.entity.TbBrand;
import com.bdqn.service.BrandService;
import com.bdqn.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/BrandServlet")
public class BrandServlet extends HttpServlet {

    private BrandService brandService = new BrandServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if ("selectAll".equals(action)) {
            handleSelectAll(request, response);
        } else if ("add".equals(action)) {
            handleAdd(request, response);
        } else if ("selectById".equals(action)) {
            handleSelectById(request, response);
        } else if ("updateForm".equals(action)) {
            handleUpdateForm(request, response);
        } else if ("update".equals(action)) {
            handleUpdate(request, response);
        } else if ("delete".equals(action)) {
            handleDelete(request, response);
        }
    }

    private void handleSelectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<TbBrand> brands = brandService.selectAll();
        request.setAttribute("brands", brands);
        request.getRequestDispatcher("brand.jsp").forward(request, response);
    }

    private void handleAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brandName = request.getParameter("brandName");
        String companyName = request.getParameter("companyName");
        int ordered = Integer.parseInt(request.getParameter("ordered"));
        String description = request.getParameter("description");
        int status = Integer.parseInt(request.getParameter("status"));

        TbBrand brand = new TbBrand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        brand.setStatus(status);

        int result = brandService.add(brand);
        response.sendRedirect(request.getContextPath() + "/BrandServlet?action=selectAll");
    }

    private void handleSelectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        TbBrand brand = brandService.selectById(id);
        request.setAttribute("brand", brand);
        request.getRequestDispatcher("brand.jsp").forward(request, response);
    }

    private void handleUpdateForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        TbBrand brand = brandService.selectById(id);
        request.setAttribute("updateBrand", brand);
        request.getRequestDispatcher("brand.jsp").forward(request, response);
    }

    private void handleUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String brandName = request.getParameter("brandName");
        String companyName = request.getParameter("companyName");
        int ordered = Integer.parseInt(request.getParameter("ordered"));
        String description = request.getParameter("description");
        int status = Integer.parseInt(request.getParameter("status"));

        TbBrand brand = new TbBrand();
        brand.setId(id);
        brand.setCompanyName(companyName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        brand.setStatus(status);

        int result = brandService.update(brand);
        if (result > 0) {
            response.sendRedirect(request.getContextPath() + "/BrandServlet?action=selectAll");
        } else {
            response.sendRedirect(request.getContextPath() + "/BrandServlet?action=selectAll&error=updateFailed");
        }
    }

    private void handleDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        boolean result = brandService.delete(id);
        response.sendRedirect(request.getContextPath() + "/BrandServlet?action=selectAll");
    }
}
